package com.frame.mysql.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @author chai
 * @date 2020/6/26
 * @description
 */
@Configuration
public class DataSourceConfig {

    @Value("${mysql.datasource.driver}")
    private String driver;

    @Value("${mysql.datasource.url}")
    private String url;

    @Value("${mysql.datasource.username}")
    private String userName;

    @Value("${mysql.datasource.password}")
    private String passWord;

    @Bean
    @ConditionalOnMissingBean(DataSource.class)
    public DataSource primaryDataSource(){
        DruidDataSource source = new DruidDataSource();
        source.setDriverClassName(driver);
        source.setUrl(url);
        source.setUsername(userName);
        source.setPassword(passWord);
        return source;
    }

}
